package com.linkcard.list.action;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkcard.commen.tools.Helper;
import com.linkcard.commen.tools.Page;
import com.linkcard.list.manager.CategoryManager;
import com.linkcard.list.model.Category;
import com.opensymphony.xwork2.ActionSupport;

public class CategoryAction extends ActionSupport {

	private CategoryManager categoryManager=null;
	private Category category=null;
	private int page;
	private int rows;
	@Override
	public String execute() throws Exception {
		return "success";
	}
	//添加类别
	public String addCategory() throws Exception{
		  category.setName(Helper.decode(category.getName()));
		  boolean isSuccess=this.categoryManager.addCategory(category);
		  JSONObject json=new JSONObject();
		  json.put("info",isSuccess);
		  backText(json.toString());
		return "Data";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	//获得类别列表
	public String categoryList() throws Exception {
		Page pageObj=new Page();
		     pageObj.setCurrentPage(page);
		     pageObj.setPageSize(rows);
		List list=this.categoryManager.categoryList(category.getColumnId(),-1);
		JSONArray jsonArray=new JSONArray();
		 for(int i=0;i<list.size();i++)
		 {
			 JSONObject jsonObject=new JSONObject();
			 Category category=(Category)list.get(i);
			 jsonObject.put("id",category.getId());
			 jsonObject.put("name",category.getName());
			 jsonObject.put("sort",category.getSort());
			 jsonObject.put("columnId",category.getColumnId());
			 jsonObject.put("opt","<a href='javascript:void(0);' onclick='append("+category.getId()+","+category.getColumnId()+",\""+category.getName()+"\")'>添加子类别</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='modify("+category.getId()+",\""+category.getName()+"\",\"无\","+category.getSort()+")'>修改</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='deleteCategory("+category.getId()+")'>删除</a>");
		     List subList= this.categoryManager.categoryList(category.getColumnId(),category.getId());
		     if(subList.size()!=0)
		     {
		    	 jsonObject.put("state","closed"); 
			 JSONArray subJsonArray=new JSONArray();
			 for(int j=0;j<subList.size();j++)
			 {
				 Category subCategory=(Category)subList.get(j);
				 JSONObject subJsonObject=new JSONObject();
				 subJsonObject.put("id",subCategory.getId());
				 subJsonObject.put("name",subCategory.getName());
				 subJsonObject.put("sort",subCategory.getSort());
				 subJsonObject.put("columnId",subCategory.getColumnId());
				 subJsonObject.put("opt","<a href='javascript:void(0);' onclick='modify("+subCategory.getId()+",\""+subCategory.getName()+"\",\""+category.getName()+"\","+subCategory.getSort()+")'>修改</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='deleteCategory("+subCategory.getId()+")'>删除</a>");
				 subJsonArray.add(subJsonObject);
			 }
			
			 jsonObject.put("children",subJsonArray);
		     }
			 jsonArray.add(jsonObject);
		 }
		 backText(jsonArray.toString());
		return "Data";
	}
	//修改类别
	public String modifyCategory() throws Exception 
	{
		category.setName(Helper.decode(category.getName()));
		boolean isSuccess=this.categoryManager.modifyCategory(category);
		JSONObject json=new JSONObject();
		  json.put("info",isSuccess);
		  backText(json.toString());
		return "Data";
	}
	//删除类别
	public String deleteCategory() throws Exception{
		boolean isSuccess=this.categoryManager.deleteCategory(category);
		JSONObject json=new JSONObject();
		  json.put("info",isSuccess);
		  backText(json.toString());
		return "Data";
	}
	//获得类别列表
	public String categoryForList()throws Exception{
		List list=this.categoryManager.categoryList(category.getColumnId(),category.getId());
		JSONArray json=JSONArray.fromObject(list);
		 backText(json.toString());
		return "Data";
	}
	
	public CategoryManager getCategoryManager() {
		return categoryManager;
	}
	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}

}
